package com.venutech.gongan.api;

import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.security.LoginUser;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.venutech.gongan.entity.TbWork;
import com.venutech.gongan.service.ITbWorkService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 巡检Controller
 * 
 * @author linxb
 * @date 2023-09-18
 */
@Api(value = "巡检报表管理", tags = "巡检报表管理")
@RestController
@RequestMapping("/gongan/tbWork")
public class TbWorkController extends BaseController
{
    private String prefix = "gongan/tbWork";

    @Autowired
    private ITbWorkService tbWorkService;

    @GetMapping()
    public String work()
    {
        return prefix + "/tbWork";
    }

    /**
     * 查询巡检列表
     */
    @ApiOperation("查询巡检管理列表")
    @GetMapping("/list")
    @ResponseBody
    public TableDataInfo list(TbWork tbWork)
    {
        startPage();
        List<TbWork> list = tbWorkService.selectTbWorkList(tbWork);
        return getDataTable(list);
    }

    @ApiOperation("查询巡检详情")
    @GetMapping("/getTbWork")
    @ResponseBody
    public AjaxResult getTbWorkOrder(TbWork tbWork) {
        List<TbWork> list = tbWorkService.selectTbWorkList(tbWork);
        return AjaxResult.success(list);
    }

    /**
     * 查询故障工单列表
     */
    @ApiOperation("查询故障工单列表")
    @GetMapping("/faultyWorkList")
    @ResponseBody
    public TableDataInfo faultyWorkList(TbWork tbWork)
    {
        startPage();
        List<TbWork> list = tbWorkService.faultyWorkList(tbWork);
        return getDataTable(list);
    }
    /**
     * 查询故障工单列表
     */
    @ApiOperation("查询告警清单列表")
    @GetMapping("/faultyIntelWorkList")
    @ResponseBody
    public TableDataInfo faultyIntelWorkList(TbWork tbWork)
    {
        startPage();
        List<TbWork> list = tbWorkService.faultyIntelWorkList(tbWork);
        return getDataTable(list);
    }
    /**
     * 智能巡检
     */
    @ApiOperation("智能巡检列表")
    @GetMapping("/intelWorkList")
    @ResponseBody
    public TableDataInfo intelWorkList(TbWork tbWork)
    {
        startPage();
        List<TbWork> list = tbWorkService.intelWorkList(tbWork);
        return getDataTable(list);
    }
    /**
     * 查询巡检列表
     */
    @ApiOperation("查询巡检、故障、运维待办列表")
    @GetMapping("/remainOpWorkList")
    @ResponseBody
    public TableDataInfo remainOpWorkList(TbWork tbWork)
    {
        startPage();
        LoginUser loginUser = SecurityUtils.getLoginUser();
        tbWork.setWkSpUserId(loginUser.getUserId());
        tbWork.setWkState((long)0);
        List<TbWork> list = tbWorkService.selectTbWorkList(tbWork);
        return getDataTable(list);
    }

    /**
     * 查询巡检列表
     */
    @ApiOperation("查询巡检、故障、运维已办列表")
    @GetMapping("/completedOpWorkList")
    @ResponseBody
    public TableDataInfo completedOpWorkList(TbWork tbWork)
    {
        startPage();
        LoginUser loginUser = SecurityUtils.getLoginUser();
        tbWork.setWkSpUserId(loginUser.getUserId());
        List<TbWork> list = tbWorkService.selectcompletedTbWorkList(tbWork);
        return getDataTable(list);
    }
    /**
     * 查询巡检列表
     */
    @ApiOperation("查询巡检、故障、运维申请列表")
    @GetMapping("/applyOpWorkList")
    @ResponseBody
    public TableDataInfo applyOpWorkList(TbWork tbWork)
    {
        startPage();
        LoginUser loginUser = SecurityUtils.getLoginUser();
        tbWork.setWkSqUserId(loginUser.getUserId());
        //tbWork.setWkState((long)0);
        List<TbWork> list = tbWorkService.selectTbWorkList(tbWork);
        return getDataTable(list);
    }

    /**
     * 导出巡检列表
     */
    @ApiOperation("导出巡检列表")
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(@RequestBody TbWork tbWork)
    {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        tbWork.setWkSqUserId(loginUser.getUserId());
        List<TbWork> list = tbWorkService.selectTbWorkList(tbWork);
        ExcelUtil<TbWork> util = new ExcelUtil<TbWork>(TbWork.class);
        String sheetName="巡检工单";
        if(tbWork.getExportSqTime()!=null){
            sheetName=sheetName+tbWork.getExportSqTime();
        }
        return util.exportExcel(list, sheetName);
    }



//    @ApiOperation("导出日报表")
//    @GetMapping("/exportPatrolWorkExcelDaily/{day}")
//    public void exportPatrolWorkExcelDaily(@PathVariable("day")String day, HttpServletResponse response) throws Exception {
//        Date startDate = DateTimeUtil.formatDate(day, "yyyy-MM-dd");
//        Date endDate = DateTimeUtil.addDate(startDate,1);
//        String fileName = String.format("巡检工单日报表(%s).xls", DateTimeUtil.dateToString(startDate, "yyyyMMdd"));
//        generatePatrolWorkReportExcel(startDate, endDate, fileName, response);
//
//    }
//    @ApiOperation("导出周报表")
//    @GetMapping("/exportPatrolWorkExcelWeekly/{startDay}")
//    public void exportPatrolWorkExcelWeekly(@PathVariable("startDay")String startDay, HttpServletResponse response) throws Exception {
//        Date startDate =  DateTimeUtil.formatDate(startDay, "yyyy-MM-dd");
//        Date endDate = DateTimeUtil.addDate(startDate,7);
//        String fileName = String.format("巡检工单周报表(%s-%s).xls", DateTimeUtil.dateToString(startDate, "yyyyMMdd"), DateTimeUtil.dateToString(DateTimeUtil.addDate(startDate,6), "yyyyMMdd"));
//        generatePatrolWorkReportExcel(startDate, endDate, fileName, response);
//    }
//    @ApiOperation("导出月报表")
//    @GetMapping("/exportPatrolWorkExcelMonthly/{startMonth}")
//    public void exportPatrolWorkExcelMonthly(@PathVariable("startMonth") String startMonth, HttpServletResponse response) throws Exception {
//        Date startDate = DateTimeUtil.formatDate(String.format("%s-01", startMonth), "yyyy-MM-dd");
//        Calendar calendar = Calendar.getInstance();
//        calendar.setTime(startDate);
//        calendar.add(Calendar.MONTH, 1);
//        Date endDate = calendar.getTime();
//        String fileName = String.format("巡检工单月报表(%s).xls", startMonth);
//        generatePatrolWorkReportExcel(startDate, endDate, fileName, response);
//    }
//    private void generatePatrolWorkReportExcel(Date startDate, Date endDate, String fileName, HttpServletResponse response)  throws Exception  {
//        HashMap<String, List<List<String>>> sheetMap = new HashMap<>();
//        List<Work> patrolWorks = workService.getByTypeAndDateRange(Constants.WORK_TYPE_PATROL, startDate, endDate);
//        List<List<String>> patrolRows = assemblePatrolWorkReportExcelData(patrolWorks, Constants.WORK_TYPE_PATROL);
//        sheetMap.put("巡检打卡", patrolRows);
//
//        List<List<String>> patrolPercentRows = caculatePatrolPercent(patrolWorks, startDate, endDate);
//        sheetMap.put("打卡率统计", patrolPercentRows);
//
//        ExcelUtils.exportMultiSheetExcel(sheetMap, fileName, response);
//    }


    /**
     * 新增巡检
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存巡检
     */
    @ApiOperation("新增保存巡检")
    @PostMapping("/addTbWork")
    @ResponseBody
    public AjaxResult addSave(@RequestBody TbWork tbWork)
    {
        return toAjax(tbWorkService.insertTbWork(tbWork));
    }

    /**
     * 修改巡检
     */
    @GetMapping("/edit/{wkId}")
    public String edit(@PathVariable("wkId") Long wkId, ModelMap mmap)
    {
        TbWork tbWork = tbWorkService.selectTbWorkByWkId(wkId);
        mmap.put("tbWork", tbWork);
        return prefix + "/edit";
    }

    /**
     * 修改保存巡检
     */
    @ApiOperation("修改保存巡检")
    @PostMapping("/editTbWork")
    @ResponseBody
    public AjaxResult editSave(@RequestBody TbWork tbWork)
    {
        return toAjax(tbWorkService.updateTbWork(tbWork));
    }

    /**
     * 删除巡检
     */

    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(tbWorkService.deleteTbWorkByWkIds(ids));
    }

    /**
     * 删除巡检
     */
    @ApiOperation("删除巡检")
    @GetMapping( "/deleteTbWorkByWkId/{id}")
    @ResponseBody
    public AjaxResult deleteTbOpWorkByOpWorkId(@PathVariable Long id)
    {
        return toAjax(tbWorkService.deleteTbWorkByWkId(id));
    }
}
